package com.service.purchase;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.excel.EasyExcel;
import com.common.StringUtils;
import com.common.easyui.EasyuiUtils;
import com.common.http.HttpResult;
import com.common.http.HttpStatus;
import com.dao.purchase.SupplyDealMapper;
import com.enums.BtnDbPermissionEnum;
import com.enums.SysModuleEnum;
import com.model.purchase.PurchaseSupplyDeal;
import com.model.sys.SysUser;
import com.service.sys.SysUserService;
import com.common.model.Global;
import com.common.spring.SpringMvcUtils;

@Service
public class SupplyDealService {
	static final Logger logger = LoggerFactory.getLogger(SupplyDealService.class);

	@Autowired
	SupplyDealMapper supplyDealDao;
	@Autowired
	SysUserService sysUserService;

	/**
	 * 列表查询
	 *
	 * @param queryMap
	 * @return
	 */
	public HttpResult selectList(Map<String, Object> queryMap) {
		HttpResult httpResult = new HttpResult();
		EasyuiUtils.addPageNum(queryMap);
		EasyuiUtils.addFilter(queryMap);

		SysUser sysUser = SpringMvcUtils.getSysUser();
		// 增加合同执行人查询判断，采购订单处使用
		if (queryMap.get("inqActualId") == null) {
			if (!sysUser.getSuperadmin().equals(Global.YES + "")) {
				if (StringUtils.isNotNull(sysUser.getDatapermission())) {
					queryMap.put("deptIds", sysUser.getDatapermission().split(","));
				} else {
//					queryMap.put("createId", sysUser.getUserid());
					if (sysUser.getBtnPermission() != null
							&& sysUser.getBtnPermission().contains(BtnDbPermissionEnum.purchase.getValue())) {
						List<String> roleList = sysUserService.getPermissionFromModuleAndUser(sysUser.getUserid(),
								SysModuleEnum.InquiryResult.getValue());
						if (roleList != null && roleList.size() > 0) {
							queryMap.put("deptIds", roleList);
						}
					} else {
						queryMap.put("createId", sysUser.getUserid());
					}
				}

			}
		}
		List<PurchaseSupplyDeal> list = supplyDealDao.selectList(queryMap);
		int count = supplyDealDao.selectListCount(queryMap);
		httpResult.setRows(list);
		httpResult.setTotal(count);
		logger.info("selectList:{}", list);
		return httpResult;
	}

	/**
	 * 详情
	 * 
	 * @param
	 * @return
	 */
	public HttpResult selectInfor(String infId, String resId) {
		HttpResult httpResult = new HttpResult();
		PurchaseSupplyDeal supplyDealModel = supplyDealDao.load(infId, resId);
		httpResult.setData(supplyDealModel);
		httpResult.setCode(HttpStatus.SUCCESS_CODE);
		return httpResult;
	}

	/**
	 * 保存
	 *
	 * @param
	 * @return
	 */
	public HttpResult save(PurchaseSupplyDeal supplyDealModel) {
		if (supplyDealModel == null) {
			return HttpResult.error("参数不能为空");
		}
		HttpResult httpResult = new HttpResult();
		int result = 0;
		if (!StringUtils.isNotNull(supplyDealModel.getInfId())) {
			supplyDealModel.setInfId(UUID.randomUUID().toString());
			result = supplyDealDao.insert(supplyDealModel);
		} else {
			result = supplyDealDao.update(supplyDealModel);
		}
		if (!StringUtils.isNotNull(supplyDealModel.getResId())) {
			supplyDealModel.setResId(UUID.randomUUID().toString());
			result = supplyDealDao.insert(supplyDealModel);
		} else {
			result = supplyDealDao.update(supplyDealModel);
		}
		if (result > 0) {
			httpResult.setMsg(supplyDealModel.getStatus().equals(Global.YES + "") ? "提交成功!" : "保存成功!");
			httpResult.setData(supplyDealModel);
		} else {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg(supplyDealModel.getStatus().equals(Global.YES + "") ? "提交失败!" : "保存失败!");
		}
		return httpResult;
	}

	/**
	 * 删除
	 *
	 * @param
	 * @return
	 */
	public HttpResult delete(String infId, String resId) {
		if (!StringUtils.isNotNull(infId) || !StringUtils.isNotNull(resId)) {
			return HttpResult.error("参数不能为空");
		}
		PurchaseSupplyDeal model = supplyDealDao.load(resId, infId);
		if (!model.getStatus().equals(Global.NO + "")) {
			return HttpResult.error("非待提交状态，不可删除！");
		}
		HttpResult httpResult = new HttpResult();
		int result = supplyDealDao.delete(infId, resId);
		if (result > 0) {
			httpResult.setMsg("删除成功！");
		} else {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg("不存在此条数据！");
		}
		return httpResult;
	}

	/**
	 * 导出
	 * 
	 * @param queryMap
	 * @param response
	 */
	public void export(Map<String, Object> queryMap, HttpServletResponse response) {
		SysUser sysUser = SpringMvcUtils.getSysUser();
		// 增加合同执行人查询判断，采购订单处使用
		if (queryMap.get("inqActualId") == null) {
			if (!sysUser.getSuperadmin().equals(Global.YES + "")) {
				if (StringUtils.isNotNull(sysUser.getDatapermission())) {
					queryMap.put("deptIds", sysUser.getDatapermission().split(","));
				} else {
//					queryMap.put("createId", sysUser.getUserid());
					if (sysUser.getBtnPermission() != null
							&& sysUser.getBtnPermission().contains(BtnDbPermissionEnum.purchase.getValue())) {
						List<String> roleList = sysUserService.getPermissionFromModuleAndUser(sysUser.getUserid(),
								SysModuleEnum.InquiryResult.getValue());
						if (roleList != null && roleList.size() > 0) {
							queryMap.put("deptIds", roleList);
						}
					} else {
						queryMap.put("createId", sysUser.getUserid());
					}
				}

			}
		}
		List<PurchaseSupplyDeal> list = supplyDealDao.selectList(queryMap);
		logger.info("export:{}", list);
		try {
			EasyExcel.write(response.getOutputStream(), PurchaseSupplyDeal.class).sheet("模板").doWrite(list);
		} catch (IOException e) {
			logger.error("导出错误:{}", e.getMessage());
		}

	}
}
